/*
 *  JDiv - API for Multimedia Application
 *  http://jdiv.sourceforge.net 
 *  ------------------------------------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

package com.jdiv.functions;

import com.badlogic.gdx.Gdx;
import com.jdiv.Scene;
import com.jdiv.kernel.JDivResources;
import com.jdiv.kernel.JDivSystem;

/** Class that handles data Input.*/
public class FunctionsInput {
	
	 /** 
	  * @return If touched screen return true and else return false.
	  */
	 public boolean isTouched(){
		return JDivSystem.touch.isTouched();
	 }
	 
	 /** 	
	  * @return If touch up screen return true and else return false. Similar to "isTouchUp(0)".
	  */
	 
	 public boolean isTouchUp(){
	  return isTouchUp(0);
	 }
	 
	 /**
      * Whether the screen is currently touched by the pointer with the given index. Pointers are indexed from 0 to n. The pointer id identifies the order in which the fingers went down on the screen, e.g. 0 is the first finger, 1 is the second and so on. When two fingers are touched down and the first one is lifted the second one keeps its index. If another finger is placed on the touch screen the first free index will be used. 
	  * @param pointer Number of the pointer.
	  * @return If touch up screen return true and else return false by the pointer.
	  */
	 
	 public boolean isTouchUp( int pointer ){
		 if( (pointer >= 0) && (pointer < JDivSystem.touch.MAX_TOUCH) )
			 return JDivSystem.touch.isTouchUp(pointer);
		 else
			 return false;
	 }
	 
	 /** 	
	  * @return If touch down screen return true and else return false. Similar to "isTouchDown(0)".
	  */
	 
	 public boolean isTouchDown(){
	  return isTouchDown(0);
	 }
	 
	 /**
      * Whether the screen is currently touched by the pointer with the given index. Pointers are indexed from 0 to n. The pointer id identifies the order in which the fingers went down on the screen, e.g. 0 is the first finger, 1 is the second and so on. When two fingers are touched down and the first one is lifted the second one keeps its index. If another finger is placed on the touch screen the first free index will be used. 
	  * @param pointer Number of the pointer.
	  * @return If touch down screen return true and else return false by the pointer.
	  */
	 
	 public boolean isTouchDown( int pointer ){
		 if( (pointer >= 0) && (pointer < JDivSystem.touch.MAX_TOUCH) )
			 return JDivSystem.touch.isTouchDown(pointer);
		 else
			 return false;
	 }
	 
	 /** 
	  * @return Coordinate of X touched in scene 0. Similar to "getTouchSceneX(0)".
	  */
	 
	 public float getTouchX(){
		 return getTouchSceneX(0);
	 }
	 
	 /**  
	  * @param pointer Number of the pointer.
	  * @return Coordinate of X touched in scene 0 by the pointer. Similar to "getTouchSceneX(pointer, 0)".
	  */
	 
	 public float getTouchX( int pointer ){
		 return getTouchSceneX(pointer, 0);
	 }
	 
	 /**  
	  * @param pointer Number of the pointer.
	  * @param idScene Number identifier of the Scene.
	  * @return Coordinate of X touched in scene by the pointer.
	  */
	 
	 public float getTouchSceneX(int pointer, int idScene){
		 
	  if( (pointer >= 0) && (pointer < JDivSystem.touch.MAX_TOUCH) ){
	   Scene scene = JDivResources.listScenes.getJDivScene(idScene);
	   
	   if( scene == null ) return 0;
		  
	   float ratioX = JDivSystem.getScenesWidth()/Gdx.graphics.getWidth();
	   
	   return ( ( JDivSystem.touch.getX(pointer)*ratioX ) // Screen Position 
			    - scene.getScrollX()*scene.getScale() - scene.getReserved().offsetCX )/scene.getScale(); // Scene Position
			     
	
	  }else
		  return 0;
	  
	 }
	 
	 /**  
	  * @param idScene Number identifier of the Scene.
	  * @return Coordinate of X touched in scene by the pointer.
	  */
	 
	 public float getTouchSceneX( int idScene ){ 
	  
	   Scene scene = JDivResources.listScenes.getJDivScene(idScene);
	   
	   if( scene == null ) return 0;
		  
	   float ratioX = JDivSystem.getScenesWidth()/Gdx.graphics.getWidth();
	   
	   return ( ( JDivSystem.touch.getX()*ratioX ) // Screen Position 
			    - scene.getScrollX()*scene.getScale() - scene.getReserved().offsetCX )/scene.getScale(); // Scene Position
	  
	 }
	 
	 /**  
	  * @return Coordinate of X touched in screen by the pointer.
	  */
	 
	 public float getTouchScreenX(){
		 float ratioX = JDivSystem.getScenesWidth()/Gdx.graphics.getWidth();
		   
		 return JDivSystem.touch.getX()*ratioX;
	 }
	 
	 /**  
	  * @param pointer Number of the pointer.
	  * @return Coordinate of X touched in screen by the pointer.
	  */
	 
	 public float getTouchScreenX(int pointer){
		 
	  if( (pointer >= 0) && (pointer < JDivSystem.touch.MAX_TOUCH) ){  
	   float ratioX = JDivSystem.getScenesWidth()/Gdx.graphics.getWidth();
	   
	   return JDivSystem.touch.getX(pointer)*ratioX;
	  }else
		  return 0;
	  
	 }
	 
	 /** 
	  * @return Coordinate of Y touched in scene 0. Similar to "getTouchSceneY(0)".
	  */
	 
	 public float getTouchY(){
		 return Gdx.graphics.getHeight() - getTouchSceneY(0);
	 }
	 
	 /**
	  * @param pointer Number of the pointer. 
	  * @return Coordinate of Y touched in scene 0. 
	  */
	 
	 public float getTouchY( int pointer ){
		 return getTouchSceneY( pointer, 0 );
	 }
	 
	 /**
	  * @param pointer Number of the pointer. 
	  * @param idScene Number identifier of the Scene.
	  * @return Coordinate of Y touched in scene.
	  */
	 
	 public float getTouchSceneY( int pointer, int idScene ){
	 	 
	  if( (pointer >= 0) && (pointer < JDivSystem.touch.MAX_TOUCH) ){
	   Scene scene = JDivResources.listScenes.getJDivScene(idScene);
		   
	   if( scene == null ) return 0;
		  
	   float ratioY = JDivSystem.getScenesHeight()/Gdx.graphics.getHeight();
	   
	   return ( ( (Gdx.graphics.getHeight() - JDivSystem.touch.getY(pointer))*ratioY ) // Screen position 
			    - scene.getScrollY()*scene.getScale() - scene.getReserved().offsetCY )/scene.getScale(); // Scene Position
		
	  }else
		  return 0;
	  
	 }
	 
	 /** 
	  * @param idScene Number identifier of the Scene.
	  * @return Coordinate of Y touched in scene.
	  */
	 
	 public float getTouchSceneY( int idScene ){
	  Scene scene = JDivResources.listScenes.getJDivScene(idScene);
		   
	  if( scene == null ) return 0;
		 
	  float ratioY = JDivSystem.getScenesHeight()/Gdx.graphics.getHeight();
	  
	  return ( ( (Gdx.graphics.getHeight() - JDivSystem.touch.getY())*ratioY ) // Screen position 
			    - scene.getScrollY()*scene.getScale() - scene.getReserved().offsetCY )/scene.getScale(); // Scene Position
	  
	 }
	 
	 /**
	  * @return Coordinate of Y touched in screen.
	  */
	 
	 public float getTouchScreenY(){
		 float ratioY = JDivSystem.getScenesHeight()/Gdx.graphics.getHeight();
		   
		 return (Gdx.graphics.getHeight() - JDivSystem.touch.getY())*ratioY; // Screen position 
	 }
	 
	 /**
	  * @param pointer Number of the pointer. 
	  * @return Coordinate of Y touched in screen.
	  */
	 
	 public float getTouchScreenY( int pointer ){
		 
	  if( (pointer >= 0) && (pointer < JDivSystem.touch.MAX_TOUCH) ){
	   float ratioY = JDivSystem.getScenesHeight()/Gdx.graphics.getHeight();
	   
	   return (Gdx.graphics.getHeight() - JDivSystem.touch.getY(pointer))*ratioY; // Screen position 
			   
	  }else
		  return 0;
	  
	 }
	 
	 /**
	  * @return Time it touched the screen. Similar to "getTouchTime(0)".
	  */
	 
	 public float getTouchTime(){
		 return getTouchTime(0);
	 }
	 
	 /**
	  * @param pointer Number of the pointer.
	  * @return Time it touched the screen by the pointer.
	  */
	 
	 public float getTouchTime(int pointer){
		 
		if( (pointer >= 0) && (pointer < JDivSystem.touch.MAX_TOUCH) )
		 return JDivSystem.touch.getTouchTime(pointer);
		else
		 return 0;
		
	 }
	 
	 /**
	  * @return If I press the left mouse button. Note that on Android we only emulate the left mouse button. Any touch event will be interpreted as if it was issued with a left mouse button press. Touch screens obviously don't have a notion of left, right and middle button. 
	  */
	 
	 public boolean isMousePressedLeft(){
	  return JDivSystem.touch.isLeftPressed();
	 }
	 
	 /**
	  * @return If I press the middle mouse button. Note that on Android we only emulate the left mouse button. Any touch event will be interpreted as if it was issued with a left mouse button press. Touch screens obviously don't have a notion of left, right and middle button. 
	  */
	 
	 public boolean isMousePressedMiddle(){
	  return JDivSystem.touch.isMiddlePressed();
	 }
	 
	 /**
	  * @return If I press the right mouse button. Note that on Android we only emulate the left mouse button. Any touch event will be interpreted as if it was issued with a left mouse button press. Touch screens obviously don't have a notion of left, right and middle button. 
	  */
	 
	 public boolean isMousePressedRight(){
	  return JDivSystem.touch.isRightPressed();	 
	 }
	 
	 /**
	  * Time of vibration from device.
	  * @param timeMilliseconds Time in milliseconds.
	  */
	 
	 public void vibrate( int timeMilliseconds ){
		 Gdx.input.vibrate( timeMilliseconds );
	 }
	 
	 
	 /**
	  * If a key was pressed or not, similar to "Gdx.input.isKeyPressed()".
	  * @param KeyCode Key code, for example: "Keys.LEFT".
	  * @return if KeyCode it's pressed.
	  */
	 
	 public boolean key(int KeyCode) {
		return Gdx.input.isKeyPressed(KeyCode);
	 }
	 
	 /**
	  * Capturing the BackKey "Keys.BACK" to use in managing the program. It's used in Android.
	  * @param bBackKey True Catch Key or False not Catch Key.
	  */

	 public void catchBackKey( boolean bBackKey ){
		 JDivSystem.controlBackKey = !bBackKey;
	 }
	 
	 /**
	  * Capturing the MenuKey "Keys.MENU" to use in managing the program. It's used in Android.
	  * @param bMenuKey True Catch Key or False not Catch Key.
	  */

	 public void catchMenuKey( boolean bMenuKey ){
		 Gdx.input.setCatchMenuKey(bMenuKey);
	 }
}
